﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.ComponentModel;

namespace HyperPin_Database_Editor
{
    public class TableSummary : INotifyPropertyChanged
    {
        public static string xmlTable = "game";
        public static string xmlType = "type";
        public static string xmlName = "name";
        public static string xmlDescription = "description";
        public static string xmlYear = "year";
        public static string xmlManufacturer = "manufacturer";

        public bool isDirty = false;
        public bool isValid = true;

        public TableSummary()
        {
        }

        public TableSummary(XmlNode node)
        {
            xmlTableNode = node;
        }

        public bool Favorite
        {
            get { return MainForm.theMainForm.isFavoriteTable(TableName); }
            set { MainForm.theMainForm.setFavroiteTable(TableName, value); }
        }

        private XmlNode xmlTableNode;
        public XmlNode XmlNode
        {
            get { return xmlTableNode; }
        }

        public string TableName
        {
            get { return GetInnerText(xmlName); }
            set { SetInnerText(xmlName, value); }
        }

        public string TableDescription
        {
            get { return GetInnerText(xmlDescription); }
            set { 
                SetInnerText(xmlDescription, value);
                foreach (Match match in Regex.Matches(value, @"\((.*)\s(.*)\)"))
                {
                    String manufacturer = match.Groups[1].Value;
                    String year = match.Groups[2].Value;

                    TableManufacturer = manufacturer;
                    TableYear = year;
                }
            }
        }

        public string TableManufacturer
        {
            get { return GetInnerText(xmlManufacturer); }
            set { SetInnerText(xmlManufacturer, value); }
        }

        public string TableYear
        {
            get { return GetInnerText(xmlYear); }
            set { SetInnerText(xmlYear, value); }
        }

        public string TableType
        {
            get { return GetInnerText(xmlType); }
            set { SetInnerText(xmlType, value); }
        }

        public string TableXml
        {
            get
            {
                StringWriter stringWriter = new StringWriter();
                XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter);

                //set formatting options
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.Indentation = 1;
                xmlWriter.IndentChar = '\t';

                xmlWriter.WriteStartElement(xmlTable);
                xmlWriter.WriteAttributeString(xmlName, TableName);
                xmlWriter.WriteElementString(xmlDescription, TableDescription);
                xmlWriter.WriteElementString(xmlManufacturer, TableManufacturer);
                xmlWriter.WriteElementString(xmlYear, TableYear);
                xmlWriter.WriteElementString(xmlType, TableType);
                xmlWriter.WriteEndElement();

                return stringWriter.ToString();
            }
        }

        public string GetInnerText(string nodeName)
        {
            XmlNode theNode = null;
            if (nodeName == xmlName)
            {
                theNode = xmlTableNode.Attributes.GetNamedItem(nodeName);
            }
            else
            {
                theNode = xmlTableNode.SelectSingleNode(nodeName);
            }

            if (theNode != null)
            {
                return theNode.InnerText;
            }

            return null;
        }

        public void SetInnerText(string nodeName, string value)
        {
            XmlNode theNode = null;
            if (nodeName == xmlName)
            {
                theNode = xmlTableNode.Attributes.GetNamedItem(nodeName);
                if (theNode == null)
                {
                    theNode = xmlTableNode.OwnerDocument.CreateAttribute(nodeName);
                    xmlTableNode.Attributes.Append((XmlAttribute)theNode);
                }
            }
            else
            {
                theNode = xmlTableNode.SelectSingleNode(nodeName);
                if (theNode == null)
                {
                    theNode = xmlTableNode.OwnerDocument.CreateNode(XmlNodeType.Element, nodeName, null);
                    xmlTableNode.AppendChild(theNode);
                }
            }


            theNode.InnerText = value;
            isDirty = true;

            // Notify all the bound controls that this element has changed.
            OnPropertyChanged(nodeName);
            // Always notify that the XML has changed...
            OnPropertyChanged("TableXml");
        }


        // Create the OnPropertyChanged method to raise the event
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
}
